package com.test.demo.controller;

import com.google.gson.Gson;
import com.test.demo.po.Team;
import com.test.demo.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Cenoo on 2017/7/4.
 */

@Controller
@RequestMapping(value = "/Team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @RequestMapping(value = "/addTeam", method = RequestMethod.POST)
    @ResponseBody
    public Object addTeam(@RequestBody String json) {
        Gson gson = new Gson();
        Team team = gson.fromJson(json, Team.class);
        team.setCountPeople(1);
        System.out.println(gson.toJson(team));
        try {
            int result = teamService.addTeam(team);
            if (result != 0)
                return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping(value = "/join", method = RequestMethod.GET)
    @ResponseBody
    public Object joinTeam(HttpServletRequest request) {
        String uid = request.getParameter("user");
        String tid = request.getParameter("team");
        if (uid != null && tid != null) {
            try {
                int result = teamService.requestToEnqueue(Integer.parseInt(uid), Integer.parseInt(tid));
                if (result == Team.STATUS_SUCCESS)
                    return "success";
                if (result == Team.STATUS_FULL)
                    return "team_full";
                if (result == Team.STATUS_NOT_EXIST)
                    return "team_not_exist";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "error";
    }

    @RequestMapping(value = "/exit", method = RequestMethod.GET)
    @ResponseBody
    public Object exitTeam(HttpServletRequest request) {
        String tid = request.getParameter("team");
        String uid = request.getParameter("user");
        if (tid != null && uid != null) {
            try {
                int result = teamService.exitTeam(Integer.parseInt(uid), Integer.parseInt(tid));
                if (result == Team.STATUS_BREAKED || result == Team.STATUS_SUCCESS) {
                    return  "success";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "error";
    }

    @RequestMapping(value = "/kickOut", method = RequestMethod.GET)
    @ResponseBody
    public Object kickOutUser(HttpServletRequest request) {
        String cid = request.getParameter("captain");
        String uid = request.getParameter("user");
        String tid = request.getParameter("team");
        if (cid != null && uid != null && tid != null) {
            try {
                int result = teamService.kickOut(Integer.parseInt(cid), Integer.parseInt(uid), Integer.parseInt(tid));
                if (result == Team.STATUS_SUCCESS)
                    return "success";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "error";
    }

    @RequestMapping(value = "/breakRank", method = RequestMethod.GET)
    @ResponseBody
    public Object breakRank(HttpServletRequest request) {
        String cid = request.getParameter("captain");
        String tid = request.getParameter("team");
        if (cid != null && tid != null) {
            try {
                int result = teamService.breakRanks(Integer.parseInt(cid), Integer.parseInt(tid));
                if (result == Team.STATUS_SUCCESS)
                    return "success";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "error";
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public Object searchTeam(HttpServletRequest request) {
        String keyword = request.getParameter("keyword");
        if (keyword != null) {
            try {
                List<Team> teams = teamService.searchTeam(keyword);
                if (teams != null) {
                    Gson gson = new Gson();
                    return gson.toJson(teams);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "error";
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public Object getAllTeams(HttpServletRequest request) {
        try {
            List<Team> teams = teamService.getAllTeams();
            if (teams != null) {
                Gson gson = new Gson();
                return gson.toJson(teams);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }
}
